package com.atguigu.Flink.sql.time;

import com.atguigu.Flink.POJO.Event;
import com.atguigu.Flink.function.ClickSource;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Schema;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class Flink01_ProcessTime {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<Event> ds = env.addSource(new ClickSource());
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        //将流转换为表，并指定处理时间字段
        Schema schema = Schema.newBuilder()
                .column("user","STRING")
                .column("url","STRING")
                .column("ts","BIGINT")
                //指定处理时间字段
                .columnByExpression("pt","proctime()")
                .build();
        Table table = tableEnv.fromDataStream(ds, schema);
        table.printSchema();

        //Sql中声明，连接器中声明
        String createTable =
                " create table t1 (" +
                        " id STRING , " +
                        " vc INT, " +
                        " ts BIGINT , " +
                        " pt AS proctime()" +
                        " ) with (" +
                        " 'connector' = 'print' " +
                        ")" ;
        tableEnv.executeSql(createTable);
        tableEnv.from("t1").printSchema();


        try {
            env.execute();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
